package com.handsome.中介者模式;

import java.util.HashMap;
import java.util.Map;

/**
 * @author niechun
 * @date 2023/3/19 17:20
 */
public class ConcreteMediator implements Mediator {

    private Map<String, Colleague> colleagueMap = new HashMap<>();
    private Map<String, String> interMap = new HashMap<>();

    @Override
    public void getMessage(int state, String name) {
        //如果是闹钟发出消息
        if (colleagueMap.get(name) instanceof AlarmColleague) {
            //闹钟发出0
            if (state == 0) {
                //闹钟关闭
                ((AlarmColleague) (colleagueMap.get(name))).off();
                //电视关闭
                ((TelColleague) colleagueMap.get(interMap.get("TV"))).off();
            }
            if (state == 1) {
                //闹钟开启
                ((AlarmColleague) (colleagueMap.get(name))).on();
                //电视关闭
                ((TelColleague) colleagueMap.get(interMap.get("TV"))).off();
            }
            if (state == 3) {
                //闹钟开启
                ((AlarmColleague) (colleagueMap.get(name))).on();
                //电视关闭
                ((TelColleague) colleagueMap.get(interMap.get("TV"))).on();
            }
        }
        //如果是电视发出消息
        if (colleagueMap.get(name) instanceof TelColleague) {
            //电视发出
            if (state == 0) {

                //电视开启
                ((TelColleague) colleagueMap.get(interMap.get("TV"))).on();
                ((AlarmColleague) colleagueMap.get(interMap.get("alarm"))).destroy();
            }
            if (state == 1) {
                //电视关闭
                ((TelColleague) colleagueMap.get(interMap.get("TV"))).off();
                ((AlarmColleague) colleagueMap.get(interMap.get("alarm"))).destroy();
            }

        }


    }

    @Override
    public void register(String name, Colleague colleague) {
        colleagueMap.put(name, colleague);

        if (colleague instanceof AlarmColleague) {
            interMap.put("alarm", name);

        }
        if (colleague instanceof TelColleague) {
            interMap.put("TV", name);

        }


    }

    @Override
    public void sendMessage() {

    }
}
